<template>
  <div class="ele-body">
    <a-card :bordered="false" :title="cardTitle">
      <vxe-grid ref='orderMaterialRelationGrid' v-bind="gridOptions" class="my-grid66">

        <!--自定义按钮-->
<!--        <template #bottom>-->
<!--          <div>-->
<!--            <br/>-->
<!--            <a-alert message="Informational Notes" type="info"-->
<!--                     description="Detailed description and advices about successful copywriting."-->
<!--                     show-icon/>-->
<!--            <br/>-->
<!--          </div>-->

<!--        </template>-->

<!--        <template #operate="{ row }">-->
<!--          <a-space>-->
<!--            <a @click="editEvent(row)">修改</a>-->
<!--            <a-divider type="vertical"/>-->
<!--            <a class="ele-text-success" @click="editEvent2(row.eid)">修改独立页</a>-->
<!--            <a-divider type="vertical"/>-->
<!--            <a class="ele-text-warning" @click="viewEvent(row)">查看</a>-->
<!--            <a-divider type="vertical"/>-->
<!--            <a-popconfirm-->
<!--              title="确定要删除此记录吗？"-->
<!--              @confirm="deleteEvent(row)">-->
<!--              <a class="ele-text-danger">删除</a>-->
<!--            </a-popconfirm>-->
<!--          </a-space>-->
<!--        </template>-->


<!--        &lt;!&ndash;TODO:自定义列模板&ndash;&gt;-->
<!--        <template #dddd="{  }">-->
<!--          <a href="http://showdoc.cqcflq.com/">文档地址</a>-->
<!--        </template>-->

<!--        <template #ycs>-->
<!--          <a-space>-->
<!--            <a-button type="primary" @click="addEvent()">-->
<!--              <template #icon>-->
<!--                <plus-outlined/>-->
<!--              </template>-->
<!--              <span>新建弹窗</span>-->
<!--            </a-button>-->
<!--            <a-button type="primary" @click="addEvent2()">-->
<!--              <template #icon>-->
<!--                <plus-outlined/>-->
<!--              </template>-->
<!--              <span>新建独立页</span>-->
<!--            </a-button>-->
<!--            <a-button type="danger" @click="batchDeleteEvent()">-->
<!--              <template #icon>-->
<!--                <delete-outlined/>-->
<!--              </template>-->
<!--              <span>批量删除</span>-->
<!--            </a-button>-->
<!--            <a-button @click="gridOptions.sortConfig.multiple=!gridOptions.sortConfig.multiple">-->
<!--              <template #icon>-->
<!--                <upload-outlined/>-->
<!--              </template>-->
<!--              <span>{{ gridOptions.sortConfig.multiple === false ? '单' : '多' }}字段排序</span>-->
<!--            </a-button>-->
<!--          </a-space>-->
<!--        </template>-->
      </vxe-grid>

    </a-card>
  </div>
</template>

<script>
import {defineComponent, reactive, ref, toRefs, provide} from 'vue'//, Ref
import {VXETable} from 'vxe-table'//, VxeGridInstance, VxeGridProps
import {OrderMaterialRelationService} from "@/views/restaurant/material/orderMaterialRelation/orderMaterialRelationService";

import {useRouter} from "vue-router";


export default defineComponent({
  components: {

  },
  props:{
    orderId:Number,
    creatorName:String
  },
  setup(props) {
    const router = useRouter();
    const orderMaterialRelationGrid = ref({})// as VxeGridInstance
    // 搜索表单是否展开
    let searchExpand = ref(false)
    let proxyInfo = reactive({})
    let creatorName = toRefs(props).creatorName.value
    console.log(creatorName)
    let cardTitle = ref(`提交人：${creatorName}`)
    const where = ref({})
    const eid = ref({})
    const startIndex = ref({})
    const orderMaterialRelationListApp = reactive({
      editModalShowing: false,
      viewModalShowing: false,
      editModalForEdit: false,
      addModalForEdit: false
    })
    const setOrderId = (orderId) => {
      orderMaterialRelationListApp.orderId.value = orderId
    }
    // this.$emit('orderMaterialRelationListApp',orderMaterialRelationListApp)
    // this.$emit('setOrderId',setOrderId)
    const gridOptions = reactive({
      border: true,
      resizable: true,
      showHeaderOverflow: true,
      showOverflow: true,
      highlightHoverRow: true,
      keepSource: true,
      id: 'orderMaterialRelationsGrid',
      // maxHeight: "100%",//表格高度
      height: 800,
      rowId: 'eid',
      selectRow: null,
      showEdit: false,
      showDetails: false,
      showEditTitle: '新建',
      showDetailsTitle: '查看',
      formData: {},//自定义的
      detailData: [],
      startIndex: startIndex,
      printConfig: {
        columns: [
          {field: 'name'},
          {field: 'email'},
          {field: 'nickname'},
          {field: 'age'},
          {field: 'amount'}
        ]
      },
      /*TODO:排序配置 */
      sortConfig: {
        trigger: 'cell',
        remote: true,
        multiple: true,//多字段排序，默认单排序
        showIcon: true,
        // iconAsc: "fa fa-arrow-up fa-1x",
        // iconDesc: "fa fa-arrow-down fa-1x",
      },
      customConfig: {
        storage: true
      },
      /*TODO:过滤器配置*/
      filterConfig: {
        remote: true
      },
      /*TODO:分页配置*/
      pagerConfig: {
        pageSize: 10,
        pageSizes: [5, 10, 15, 20, 50]
      },
      /*TODO:工具栏配置*/
      // toolbarConfig: {
      //   slots: {buttons: 'ycs'},
      //   refresh: true,
      //   import: true,
      //   export: true,
      //   print: true,
      //   zoom: true,
      //   custom: true
      // },
      /*TODO:请求代理配置*/
      proxyConfig: {
        seq: true, // 启用动态序号代理，每一页的序号会根据当前页数变化
        sort: true, // 启用排序代理，当点击排序时会自动触发 query 行为
        filter: true, // 启用筛选代理，当点击筛选时会自动触发 query 行为
        form: false, // 启用表单代理，当点击表单提交按钮时会自动触发 reload 行为
        // 对应响应结果 { result: [], page: { total: 100 } }
        props: {
          result: 'data.datas', // 配置响应结果列表字段
          total: 'data.totalCount', // 配置响应结果总页数字段
          message: 'data.msg'
        },
        /*TODO:请求*/
        ajax: {
          /*TODO:分页条件查询*/
          query: ({page, sorts, filters}) => {  // 接收 Promise
            const conditions = {...where.value}
            // 处理筛选条件
            filters.forEach(({property, values}) => {
              conditions[property] = values.join('$|$')
            })
            let materialOrderId = toRefs(props).orderId.value
            console.log(materialOrderId)
            const queryParams = {filters: {materialOrderId}}
            queryParams.currentPage = page.currentPage
            queryParams.pageSize = page.pageSize
            startIndex.value = (queryParams.currentPage - 1) * queryParams.pageSize + 1
            // // 处理筛选条件
            // queryParams.filters = conditions;
            // // 处理排序条件
            queryParams.orderBys = sorts;
            //
            const $grid = orderMaterialRelationGrid.value
            $grid.queryParams = queryParams
            return OrderMaterialRelationService.findOrderMaterialRelations(queryParams).catch(error => {
              VXETable.modal.message({content: `加载数据出错，原因是：${error.message}`, status: 'error'});

            })
          },
        }
      },

      /*TODO:数据表列*/
      columns: [
        /*多选checkbox 单选seq*/
        {type: 'seq', title: '#', width: 40},
        {
          field: 'supplyMaterialName',
          title: '材料名称',
          sortable: true
        },
        {
          field: 'supplierName',
          title: '供应商名称',
          sortable: true
        },
        {
          field: 'amount',
          title: '数量(kg)',
          sortable: true
        },
        {
          field: 'price',
          title: '单价(元/kg)',
          sortable: true
        },
        {
          field: 'remark',
          title: '备注',
          sortable: true
        },

        // {title: '操作', width: 250, slots: {default: 'operate'}}
      ],


      /*TODO:复选框配置*/
      checkboxConfig: {
        labelField: 'eid',
        reserve: false,
        highlight: false,
        range: true
      },
      /*TODO:编辑验证*/
      editRules: {
        name: [
          {required: true, message: 'app.body.valid.rName'},
          {min: 3, max: 50, message: '名称长度在 3 到 50 个字符'}
        ],
        email: [
          {required: true, message: '邮件必须填写'}
        ],
        role: [
          {required: true, message: '角色必须填写'}
        ]
      },
      editConfig: {
        trigger: 'click',
        mode: 'row',
        showStatus: true
      }
    })//as VxeGridProps
    provide('gridOptions', gridOptions)

    /*TODO:新建事件*/
    const addEvent = () => {
      orderMaterialRelationListApp.editModalShowing = true
      orderMaterialRelationListApp.currentId = null;
      orderMaterialRelationListApp.addModalForEdit = true;
      gridOptions.showEditTitle = "新建";
    }

    /*TODO:新建独立页*/
    const addEvent2 = () => {
      router.push(`/restaurant/material/orderMaterialRelation/orderMaterialRelation-add`);
    }
    /*TODO:编辑事件*/
    const editEvent = (row) => {
      orderMaterialRelationListApp.editModalShowing = true
      orderMaterialRelationListApp.currentId = row.eid;
      orderMaterialRelationListApp.editModalForEdit = true;
      gridOptions.showEditTitle = "编辑";
    }
    /*TODO:编辑独立页*/
    const editEvent2 = (eid) => {
      router.push(`/restaurant/material/orderMaterialRelation/orderMaterialRelation-edit/${eid}`);
    }

    /*TODO:编辑关闭事件*/
    const editEventClose = () => {
      console.log(gridOptions.formData);
      VXETable.modal.message({content: '提交成功', status: 'success'})
    }

    /*TODO:查看事件*/
    const viewEvent = (row) => {
      orderMaterialRelationListApp.currentId = row.eid;
      orderMaterialRelationListApp.viewModalShowing = true
      gridOptions.showDetailsTitle = '查看';
    }

    /*TODO:删除事件*/
    const deleteEvent = (row) => {
      OrderMaterialRelationService.deleteOrderMaterialRelation(row.eid).then(() => {
        VXETable.modal.message({content: '操作成功', status: 'success'})
      }).catch(error => {
        VXETable.modal.message({content: `系统错误，原因是：${error.message}`, status: 'error'});
      })
    }

    /*TODO:批量删除*/
    const batchDeleteEvent = () => {
      VXETable.modal.message({content: '点了批量删除', status: 'success'})
    }
    /*TODO:条件查询提交*/
    const reload = () => {
      const $grid = orderMaterialRelationGrid.value
      $grid.commitProxy('query')//TODO:执行代理方法
    }
    /*TODO:重置搜索*/
    const reset = () => {
      where.value = {}
      const $grid = orderMaterialRelationGrid.value
      $grid.commitProxy('query')//TODO:执行代理方法
    }

    //TODO:这里需要返回才能调用
    return {
      eid,
      orderMaterialRelationGrid,
      // editModal,
      gridOptions,
      searchExpand,
      cardTitle,
      addEvent,
      editEvent,
      viewEvent,
      editEventClose,
      deleteEvent,
      batchDeleteEvent,
      setOrderId,
      editEvent2,
      addEvent2,
      where,
      reload,
      reset,
      orderMaterialRelationListApp,
      ...toRefs(proxyInfo)
    }
  }
})
</script>

<style scoped>
.my-grid66 .alert-message {
  height: 40px;
  display: flex;
  align-items: center;
  margin: 10px 0;
  border-radius: 4px;
  background-color: #e6f7ff;
  border: 1px solid #91d5ff;
}

.my-grid66 .alert-message-icon {
  width: 30px;
  text-align: center;
  color: #409eff;
  margin-right: 8px;
}

.my-grid66 .alert-message-content {
  flex-grow: 1;
  padding-right: 20px;
}

.my-grid66 .page-left {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  z-index: 10;
}

.my-grid66 .select-count {
  display: inline-block;
  vertical-align: middle;
}

.my-grid66 .my-input {
  width: 100%;
}

.my-grid66 .first-col {
  position: relative;
  height: 20px;
}

.my-grid66 .first-col:before {
  content: "";
  position: absolute;
  left: -15px;
  top: 10px;
  width: 110px;
  height: 1px;
  transform: rotate(28deg);
  background-color: #e8eaec;
}

.my-grid66 .first-col .first-col-top {
  position: absolute;
  right: 4px;
  top: -10px;
}

.my-grid66 .first-col .first-col-bottom {
  position: absolute;
  left: 4px;
  bottom: -10px;
}
</style>
